/*
    This file is part of Posixcpp library.

    Posixcpp library is free software: you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public License
    as published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    Posixcpp library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU LesserGeneral Public
    License along with Posixcpp library. If not, see
    <http://www.gnu.org/licenses/>.
 */

#ifndef POSIX_IO_TTYFILEIO_INCLUDED
#define POSIX_IO_TTYFILEIO_INCLUDED

#include <termios.h>

#include "posix_io_file.hpp"

namespace posix {
namespace io {
/**
 * @class TTY
 * @brief TTY file class
 */
class TTY : public posix::io::File {
public:
    /**
     * @brief Baud rate for TTY communications
     */
    enum class BaudRate_t
    {
        BR_HANGUP = B0,
        BR_150 = B150,
        BR_300 = B300,
        BR_600 = B600,
        BR_1200 = B1200,
        BR_4800 = B4800,
        BR_9600 = B9600,
        BR_19200 = B19200,
        BR_38400 = B38400,
        BR_57600 = B57600,
        BR_115200 = B115200
    };
    
    enum class QueueSelector_t {
        INPUT = TCIFLUSH,
        OUTPUT = TCOFLUSH,
        INOUT = TCIOFLUSH
    };
    
    static BaudRate_t baudRateFromString( const std::string & str );
    
    TTY();
    /**
     * Initializing constructor
     * 
     * @param fName file name
     * @param bRate baud rate
     */
    TTY(const std::string & fName, BaudRate_t bRate = BaudRate_t::BR_HANGUP);
    ~TTY();
    
    void open() override;
    
    /**
     * @property baudRate
     * @brief baud rate for the TTY communicatons
     */
    
    BaudRate_t baudRate() {
        return m_baudRate;
    }
    void setBaudRate(BaudRate_t newVal) {
        m_baudRate = newVal;
    }

    /**
     * @brief Flush the data buffers
     */
    void flush( QueueSelector_t queues );
private:
    BaudRate_t m_baudRate;
};

std::ostream & operator << (std::ostream & stream, TTY::BaudRate_t value);
}
}
#endif // POSIX_IO_TTYFILEIO_INCLUDED
